<template>
  <div class="table1">
    <div class="btn-wrap">
      <a-button type="primary" style="margin-right: 15px;" @click="onEditForm()">添加</a-button>
      <a-button type="info" @click="download">导出</a-button>
    </div>
    <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="true" :loading="loading" :columns="columns"
      :dataSource="pageData" @selectedRow="onSelectedRow" @change="onTableChange">
      <!--操作-->
      <template slot="action" slot-scope="text, record">
        <a-button @click="onDetail(record)" type="link" style="padding:0;">查看</a-button>
      </template>
    </z-table>
    <a-modal width="600px" title="添加" :visible="addFormVisible" @ok="onAddSubmit" @cancel="onAddFormCancel">
      <a-form-model ref="addForm" :model="addForm" :label-col="{span: 4}" :wrapper-col="{span: 20}">
          <a-form-model-item label="组员名称" prop="memberId" :rules="{required: true, message: '请选择督导', trigger: ['change']}">
            <a-select v-model="addForm.memberId" :disabled="addFormTitle == '编辑'">
              <a-select-option :value="item.id" v-for="item in memberList" :key="id">{{item.memberName}}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-table :columns="formColumns" :data-source="addForm.satisfactionData" row-key="id" :pagination="false">
            <template slot-scope="text, record, index" slot="level">
              <a-select v-model="addForm.satisfactionData[index].itemLevel">
                <a-select-option :value="1">A</a-select-option>
                <a-select-option :value="2">B</a-select-option>
                <a-select-option :value="3">C</a-select-option>
                <a-select-option :value="4">D</a-select-option>
              </a-select>
            </template>
          </a-table>
          
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
  import { memberList } from '@/api/sjsk/activity/group/detail/mature/table1'
  import { getList, add, getMetrics } from '@/api/sjsk/activity/group/detail/end/table6'
  import downloadXlsl from '../../downloadXlsx.js'
  export default {
    data() {
      return {
        loading:false,
        pageFilter: {
          pageNo: 1,
          pageSize: 10
        },
        pageData: {},
        form: {},
        headerData: [],
        memberList: [],
        
        formColumns: [
          {
            title: '内容',
            dataIndex: 'itemName',
            align: 'left',
            customRender: text => {
              return <j-ellipsis value={text} length={50} />
            }
          },
          {
            title: '评价等级',
            width: 120,
            align: 'left',
            scopedSlots: { customRender: 'level' }
          }
        ],
        columns: [
          {
            title: '组员名称',
            dataIndex: 'memberName',
            align: 'left',
            customRender: text => {
              return <j-ellipsis value={text} length={50} />
            }
          },
          {
            title: '提交情况',
            dataIndex: 'submitStatus',
            align: 'left',
            customRender: text => {
              return <j-ellipsis value={text == 1 ? '已提交' : '未提交'} length={50} />
            }
          },
          {
            title: '提交日期',
            dataIndex: 'submitDate',
            align: 'left',
            customRender: text => {
              return <j-ellipsis value={text} length={50} />
            }
          },
          {
            title: '操作',
            width: 120,
            align: 'left',
            scopedSlots: { customRender: 'action' }
          }
        ],
        addFormVisible: false,
        addFormTitle: '',
        addForm: {
           "activityId": '',
            "satisfactionData": [],
            "memberId": ''
        }
      }
    },
    created() {
      this.getMemberList()
      this.getMetricsList()
      this.onQuery()
      this.addForm.activityId = this.$route.query.id
    },
    
    methods: {
      download() {
        downloadXlsl(this.$route.query.id, 13)
      },
      getMetricsList() {
        getMetrics({activityId: this.$route.query.id}).then(res => {
          const list = res.result.map(item => {
            item.itemLevel = ''
            return item
          })
          this.$set(this.addForm, 'satisfactionData', list)
          console.log(this.addForm)
        })
      },
      onEditForm(data) {
        this.addFormVisible = true
      },
      getMemberList() {
        memberList({activityId: this.$route.query.id}).then(res => {
          if(res.success) {
            this.memberList = res.result.records
          }
        })
      },
      onQuery() {
        this.pageFilter.pageNo = 1
        this.refreshTable()
      },
      onSelectedRow(keys) {
        this.selectKeys = keys
        this.isDisabled = keys.length > 0 ? false : true
      },
      onTableChange(pagination, pageFilter, sorter) {
        let keyMap = {
          currentPage: 'pageNo'
        }
        let paginations = Object.keys(pagination).reduce((newData, key) => {
          let newKey = keyMap[key] || key
          newData[newKey] = pagination[key]
          return newData
        }, {})
        this.pageFilter = Object.assign({}, this.pageFilter, paginations)
        this.refreshTable()
      },
      refreshTable() {
        let form = Object.assign({}, this.queryParam, this.pageFilter)
        form.activityId = this.$route.query.id
        this.loading = true
        getList(form).then((res) => {
            if (res.success) {
              this.pageData = res.result
              this.loading = false
              this.$refs.table.onClearRowKeys()
            } else {
              this.loading = false
            }
          })
          .catch((e) => {
            this.loading = false
            this.$message.warn('请求失败！')
          })
      },
      onAddSubmit() {
        this.$refs.addForm.validate().then(res => {
          this.addForm.satisfactionData.map(item => {
            item.activityId = this.addForm.activityId
            item.memberId = this.addForm.memberId
            item.itemId = item.id
            delete item.id
          })
          add(this.addForm).then(res => {
            if(res.success) {
              this.$message.success(res.message)
              this.onAddFormCancel()
            } else {
              this.$message.error(res.message)
            }
          })
        })
      },
      onAddFormCancel() {
        this.addForm = {
            "satisfactionData": [],
            "memberId": '',
            "activityId": this.$route.query.id,
        }
        this.getMetricsList()
        this.addFormVisible = false
      },
    }
  }
</script>

<style lang="less" scoped>
  .table1 {
    box-sizing: border-box;
    padding: 20px 30px;
    .btn-wrap{
      margin-bottom: 20px;
    }
  }
</style>
